#include <stdlib.h>
#include <stdio.h>
#include <nds.h>

#include <video_window.h>

#define WM_2D

s16 x_pos;
s16 y_pos;
s16 x_startpos;
s16 y_startpos;

void video_setStartPosition(s16 x, s16 y) {
	x_startpos=x;
	y_startpos=y;
}

void video_gotoStartPosition() {
	x_pos=x_startpos;
	y_pos=y_startpos;
}

void video_addStartPosition(s16 x, s16 y) {
	x_startpos+=x;
	y_startpos+=y;
}

#ifdef WM_2D

inline void video_putDot(s16 x, s16 y, u16 color) {
	if(LIMIT(0, x, 255)!=x || LIMIT(0, y, 191)!=y) return;	//verify that it isn't out of the screen

	MAIN[(y+y_pos)*256+(x+x_pos)]=color;
}

void video_putLine(s16 sx, s16 sy, s16 ex, s16 ey, u16 color) {
	s16 i, width, height;

	if(ex<sx) {
		i=sx;
		sx=ex;
		ex=i;
	}

	if(ey<sy) {
		i=sy;
		sy=ey;
		ey=i;
	}

	width=ex-sx+1;
	height=ey-sy+1;

	if(width>height) {
		for(i=sx; i<=ex; i++) {
			video_putDot(i, sy+(i-sx)*(height-1)/(width-1), color);
		}
	}
	else {
		for(i=sy; i<=ey; i++) {
			video_putDot(sx+(i-sy)*(width-1)/(height-1), i, color);
		}
	}
}

void video_putSquare(s16 x, s16 y, s16 width, s16 height, u16 color) {
	s16 i, j;

	if(width<0) {
		width=-width;
		x-=(width-1);
	}

	if(height<0) {
		height=-height;
		y-=(height-1);
	}

	for(j=y; j<y+height; j++) {
		for(i=x; i<x+width; i++) {
			video_putDot(i, j, color);
		}
	}
}

#endif
